<%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ include file="/WEB-INF/jsp/common/include/taglibs.jsp"%>
   <style type="text/css">
.cf:after { visibility: hidden; display: block; font-size: 0; content: " "; clear: both; height: 0; }
* html .cf { zoom: 1; }
*:first-child+html .cf { zoom: 1; }

html { margin: 0; padding: 0; }
/* body { font-size: 100%; margin: 0; padding: 1.75em; font-family: 'Helvetica Neue', Arial, sans-serif; } */

h1 { font-size: 1.75em; margin: 0 0 0.6em 0; }

a { color: #2996cc; }
a:hover { text-decoration: none; }

p { line-height: 1.5em; }
.small { color: #666; font-size: 0.875em; }
.large { font-size: 1.25em; }

/**
 * Nestable
 */

.dd { position: relative; display: block; margin: 0; padding: 0; max-width: 600px; list-style: none; font-size: 13px; line-height: 20px; }

.dd-list { display: block; position: relative; margin: 0; padding: 0; list-style: none; }
.dd-list .dd-list { padding-left: 30px; }
.dd-collapsed .dd-list { display: none; }

.dd-item,
.dd-empty,
.dd-placeholder { display: block; position: relative; margin: 0; padding: 0; min-height: 20px; font-size: 13px; line-height: 20px; }

.dd-handle { display: block; height: 30px; margin: 5px 0; padding: 5px 10px; color: #333; text-decoration: none; font-weight: bold; border: 1px solid #ccc;
    background: #fafafa;
    background: -webkit-linear-gradient(top, #fafafa 0%, #eee 100%);
    background:    -moz-linear-gradient(top, #fafafa 0%, #eee 100%);
    background:         linear-gradient(top, #fafafa 0%, #eee 100%);
    -webkit-border-radius: 3px;
            border-radius: 3px;
    box-sizing: border-box; -moz-box-sizing: border-box;
}
.dd-handle:hover { color: #2ea8e5; background: #fff; }

.dd-item > button { display: block; position: relative; cursor: pointer; float: left; width: 25px; height: 20px; margin: 5px 0; padding: 0; text-indent: 100%; white-space: nowrap; overflow: hidden; border: 0; background: transparent; font-size: 12px; line-height: 1; text-align: center; font-weight: bold; }
.dd-item > button:before { content: '+'; display: block; position: absolute; width: 100%; text-align: center; text-indent: 0; }
.dd-item > button[data-action="collapse"]:before { content: '-'; }

.dd-placeholder,
.dd-empty { margin: 5px 0; padding: 0; min-height: 30px; background: #f2fbff; border: 1px dashed #b6bcbf; box-sizing: border-box; -moz-box-sizing: border-box; }
.dd-empty { border: 1px dashed #bbb; min-height: 100px; background-color: #e5e5e5;
    background-image: -webkit-linear-gradient(45deg, #fff 25%, transparent 25%, transparent 75%, #fff 75%, #fff), 
                      -webkit-linear-gradient(45deg, #fff 25%, transparent 25%, transparent 75%, #fff 75%, #fff);
    background-image:    -moz-linear-gradient(45deg, #fff 25%, transparent 25%, transparent 75%, #fff 75%, #fff), 
                         -moz-linear-gradient(45deg, #fff 25%, transparent 25%, transparent 75%, #fff 75%, #fff);
    background-image:         linear-gradient(45deg, #fff 25%, transparent 25%, transparent 75%, #fff 75%, #fff), 
                              linear-gradient(45deg, #fff 25%, transparent 25%, transparent 75%, #fff 75%, #fff);
    background-size: 60px 60px;
    background-position: 0 0, 30px 30px;
}

.dd-dragel { position: absolute; pointer-events: none; z-index: 9999; }
.dd-dragel > .dd-item .dd-handle { margin-top: 0; }
.dd-dragel .dd-handle {
    -webkit-box-shadow: 2px 4px 6px 0 rgba(0,0,0,.1);
            box-shadow: 2px 4px 6px 0 rgba(0,0,0,.1);
}

/**
 * Nestable Extras
 */

.nestable-lists { display: block; clear: both; padding: 30px 0; width: 100%; border: 0; border-top: 2px solid #ddd; border-bottom: 2px solid #ddd; }

#nestable-menu { padding: 0; margin: 20px 0; }

#nestable-output,
#nestable2-output { width: 100%; height: 7em; font-size: 0.75em; line-height: 1.333333em; font-family: Consolas, monospace; padding: 5px; box-sizing: border-box; -moz-box-sizing: border-box; }

#nestable2 .dd-handle {
    color: #fff;
    border: 1px solid #999;
    background: #bbb;
    background: -webkit-linear-gradient(top, #bbb 0%, #999 100%);
    background:    -moz-linear-gradient(top, #bbb 0%, #999 100%);
    background:         linear-gradient(top, #bbb 0%, #999 100%);
}
#nestable2 .dd-handle:hover { background: #bbb; }
#nestable2 .dd-item > button:before { color: #fff; }

@media only screen and (min-width: 700px) { 

    .dd { float: left; width: 48%; }
    .dd + .dd { margin-left: 2%; }

}

.dd-hover > .dd-handle { background: #2ea8e5 !important; }

/**
 * Nestable Draggable Handles
 */

.dd3-content { display: block; height: 30px; margin: 5px 0; padding: 5px 10px 5px 40px; color: #333; text-decoration: none; font-weight: bold; border: 1px solid #ccc;
    background: #fafafa;
    background: -webkit-linear-gradient(top, #fafafa 0%, #eee 100%);
    background:    -moz-linear-gradient(top, #fafafa 0%, #eee 100%);
    background:         linear-gradient(top, #fafafa 0%, #eee 100%);
    -webkit-border-radius: 3px;
            border-radius: 3px;
    box-sizing: border-box; -moz-box-sizing: border-box;
}
.dd3-content:hover { color: #2ea8e5; background: #fff; }

.dd-dragel > .dd3-item > .dd3-content { margin: 0; }

.dd3-item > button { margin-left: 30px; }

.dd3-handle { position: absolute; margin: 0; left: 0; top: 0; cursor: pointer; width: 30px; text-indent: 100%; white-space: nowrap; overflow: hidden;
    border: 1px solid #aaa;
    background: #ddd;
    background: -webkit-linear-gradient(top, #ddd 0%, #bbb 100%);
    background:    -moz-linear-gradient(top, #ddd 0%, #bbb 100%);
    background:         linear-gradient(top, #ddd 0%, #bbb 100%);
    border-top-right-radius: 0;
    border-bottom-right-radius: 0;
}
.dd3-handle:before { content: '≡'; display: block; position: absolute; left: 0; top: 3px; width: 100%; text-align: center; text-indent: 0; color: #fff; font-size: 20px; font-weight: normal; }
.dd3-handle:hover { background: #ddd; }

    </style>

<div class="row-fluid">
                        <div class="span6">
                            <!-- block -->
                            <div class="block">
                                <div class="navbar navbar-inner block-header">
                                    <div class="muted pull-left">메뉴 목록 <button id="btnDbSave" onclick="dbSave();" class="btn btn-primary btn-mini">저장</button></div>
<!--                                     <div class="pull-right"><span class="badge badge-info">1,234</span> -->

<!--                                     </div> -->
                                </div>
                                <div class="block-content collapse in">

        <div class="dd span10" id="nestable3">
            <ol class="dd-list" id="menuOl">
                ${menuList }
            </ol>
        </div>
                                </div>
                            </div>
                            <!-- /block -->
                        </div>
                        <div class="span6">
                            <!-- block -->
                            <div class="block">
                                <div class="navbar navbar-inner block-header">
                                    <div class="muted pull-left">상세정보</div>
<!--                                     <div class="pull-right"><span class="badge badge-info">752</span> -->

<!--                                     </div> -->
                                </div>
                                <div class="block-content collapse in">
                                    <form class="form-horizontal">
                                      <fieldset>
                                        <legend>Form Horizontal</legend>
                                        <div class="control-group">
                                          <label class="control-label" for="menu_nm">메뉴명</label>
                                          <div class="controls">
                                            <input class="input-xlarge focused" id="menu_nm" type="text" value="">
                                            <input class="hide" id="menu_id" type="text" value="">
                                          </div>
                                        </div>
                                        <div class="control-group">
                                          <label class="control-label" for="menu_type">게시판 타입</label>
                                          <div class="controls">
                                            <select id="menu_type" name="menu_type" onchange="showBtn();" >
                                              <option value="">선택하세요</option>
                                              <option value="1">일반게시판</option>
                                              <option value="2">FAQ</option>
                                              <option value="3">QnA</option>
                                              <option value="4">포토 갤러리</option>
                                              <option value="20">메뉴 url입력</option>
                                              <option value="99">html</option>
                                            </select>
                                            <button onclick="createDb();return false;" id="btnCreateDb" class="btn btn-primary hide">생성</button>
                                          </div>
                                        </div>
<!--                                         <div class="control-group"> -->
<!--                                           <label class="control-label" for="url_"></label> -->
<!--                                           <div class="controls"> -->
<!--                                             <select id="menu_type" name="menu_type" class="chzn-select"> -->
<!--                                               <option value="">선택하세요</option> -->
<!--                                               <option value="1">일반게시판</option> -->
<!--                                               <option value="2">FAQ</option> -->
<!--                                               <option value="3">QnA</option> -->
<!--                                               <option value="4">포토 갤러리</option> -->
<!--                                               <option value="99">html</option> -->
<!--                                             </select> -->
<!--                                           </div> -->
<!--                                         </div> -->
                                        <div class="control-group">
                                          <label class="control-label" for="user_write">일반사용자 쓰기여부</label>
                                          <div class="controls">
                                            <label>
                                              <input type="checkbox" id="user_write" name="user_write" value="Y"> 쓰기
                                            </label>
                                          </div>
                                        </div>
                                        <div class="control-group">
                                          <label class="control-label" for="use_yn">메뉴 사용여부</label>
                                          <div class="controls">
                                            <label>
                                              <input type="checkbox" id="use_yn" name="use_yn" value="Y"> 사용
                                            </label>
                                          </div>
                                        </div>
<!--                                         <div class="control-group success"> -->
<!--                                           <label class="control-label" for="selectError">Select with success</label> -->
<!--                                           <div class="controls"> -->
<!--                                             <select id="selectError"> -->
<!--                                               <option>1</option> -->
<!--                                               <option>2</option> -->
<!--                                               <option>3</option> -->
<!--                                               <option>4</option> -->
<!--                                               <option>5</option> -->
<!--                                             </select> -->
<!--                                             <span class="help-inline">Woohoo!</span> -->
<!--                                           </div> -->
<!--                                         </div> -->
                                        <div class="form-actions">
                                          <button onclick="saveMenu();return false;" class="btn btn-primary">저장</button>
                                          <button type="reset" class="btn">Cancel</button>
                                          <button onclick="addMenu();return false;" class="btn">신규추가</button>
                                        </div>
                                      </fieldset>
                                    </form>
                                </div>
                            </div>
                            <!-- /block -->
                        </div>
                    </div>
<div style="display: none;" >
<form id="frmSave" method="post" action="./saveMenu.do">
	<textarea name="menuList" id="menuList" ></textarea>

</form>
</div>
<script type="text/javascript">

// showBtn();" >
// <option value="">선택하세요</option>
// <option value="1">일반게시판</option>
// <option value="2">FAQ</option>
// <option value="3">QnA</option>
// <option value="4">포토 갤러리</option>
// <option value="20">메뉴 url입력</option>
// <option value="99">html</option>
// </select>
// <button onclick="createDb();return false;" id="btnCreateDb
//게시판 타입이 변경되었을경우 db 생성버튼 추가
function showBtn(){
	var brdType = $("#menu_type").val();
	if(("1"== brdType || "2"== brdType || "3"== brdType || "4"== brdType) && ""!=$("#menu_id").val() ){
		$("#btnCreateDb").show();
	}else{
		$("#btnCreateDb").hide();
	}
}

var seqChkDb = true;
//db 생성버튼
function createDb(){
	var brdType = $("#menu_type").val();
// 	alert($("#menu_id").val());
// 	return false;
	if(("1"== brdType || "2"== brdType || "3"== brdType || "4"== brdType) && ""!=$("#menu_id").val() ){
		if(seqChkDb){
			seqChkDb = false;
			var opt = {
					type : "get",
					url: "./createDb.do",
					dataType: "json",
					data : {"menu_seq" : $("#menu_id").val(),"brd_type":brdType},
					success : function (json){
						console.log(json);
						if('Y'==json.code){
							alert("DB 생성 완료");	
						}else{
							alert("DB 생성 에러");
						}
					},
					error : function(error){
						seqChk = true;
						alert('접속지연이 발생했습니다. 몇분후에 다시 확인해주세요\n'+error);
					}
				};
				$.ajax(opt);
		}else{
			alert("진행중입니다.");			
		}
	}
}

var seqChk = true;
//메뉴 추가
function addMenu(){
	if(seqChk){
		seqChk = false;
		var opt = {
				type : "get",
				url: "./getMenuSeq.do",
				dataType: "json",
				success : function (json){
					var menuSeq = json.menuSeq;
					var html = "<li class=\"dd-item dd3-item menuList"+menuSeq+"\" data-id=\""+menuSeq+"\" menu_nm=\"신규 추가\" menu_depth=\"0\" menu_url=\"\" menu_type=\"\" view_stat=\"Y\" user_write=\"\" use_yn=\"Y\">"+
					"    <div class=\"dd-handle dd3-handle\">Drag</div><div class=\"dd3-content\"><span class=\"menu_nm\">신규 추가</span> "+
					"<a href=\"#\" onclick=\"editMenu("+menuSeq+");\" class=\"icon icon-edit\"></a> "+
					"<a href=\"#\" onclick=\"deleteMenu("+menuSeq+");\" class=\"icon icon-trash\"></a>"+
					"</div></li>";
					$("#menuOl").append(html);
					$('#nestable3').nestable();
					seqChk = true;
				},
				error : function(error){
					seqChk = true;
					alert('접속지연이 발생했습니다. 몇분후에 다시 확인해주세요\n'+error);
				}
			};
			
			$.ajax(opt);
	}else{
		alert("잠시만 기다려 주세요. 처리중입니다.");
	}
}

//메뉴 저장
function saveMenu(){
	
	$(".menuList"+$("#menu_id").val()).find(".menu_nm:eq(0)").html($("#menu_nm").val());
	
	$(".menuList"+$("#menu_id").val()).attr("menu_nm",$("#menu_nm").val());
	$(".menuList"+$("#menu_id").val()).attr("menu_type",$("#menu_type").val());
	if($("#user_write").is(":checked")){
		$(".menuList"+$("#menu_id").val()).attr("user_write",'Y');
	}else{
		$(".menuList"+$("#menu_id").val()).attr("user_write",'');
	}
	if($("#use_yn").is(":checked")){
		$(".menuList"+$("#menu_id").val()).attr("use_yn",'Y');
	}else{
		$(".menuList"+$("#menu_id").val()).attr("use_yn",'');
	}
	
}

var staticDepth=0;
//depth 알아오기
function chkDepth(){
	$(".dd-item").each(function (){
		staticDepth=0;
		chkDepthLoop(this);
		$(this).attr("menu_depth",staticDepth);
	});
}


//depth 루프
function chkDepthLoop( doc){
// 	console.log(doc);
	var doc1 = $(doc);
	var docParent = $(doc1).parent().parent();
	
	if("nestable3" == doc1.attr("id")){
		return 0;
	}else{
		staticDepth++;
		chkDepthLoop(docParent);
	}
	
}

//메뉴 수정
function editMenu(menuId){
	$("#menu_nm").val($(".menuList"+menuId).find(".menu_nm").html());
	$("#menu_id").val($(".menuList"+menuId).attr("data-id"));
	$("#menu_type").val($(".menuList"+menuId).attr("menu_type"));
	if("Y"== $(".menuList"+menuId).attr("user_write") ){
		$("#user_write").prop("checked",true);
	}else{
		$("#user_write").prop("checked",false);
	}
	
	if("Y"== $(".menuList"+menuId).attr("use_yn") ){
		$("#use_yn").prop("checked",true);
	}else{
		$("#use_yn").prop("checked",false);
	}
	showBtn();
}

//메뉴 삭제
function deleteMenu(menuId){
	if(confirm("삭제하시겠습니까?")){
		$(".menuList"+menuId).remove();
	}
}

var chkBtn = false;
//디비에 저장
function dbSave(){
	if(chkBtn){
		alert("잠시만 기다리세요 진행중입니다.");
		return false;
	}
	
	if(confirm("저장하시겠습니까?")){
		$("#btnDbSave").html("진행중...");
		chkBtn=true;
		$("#menuOl").find("button").remove();
		$("#menuList").val($("#menuOl").html());
		chkDepth();
// 		var html = "<li class=\"dd-item dd3-item menuList"+menuSeq+"\" data-id=\""+menuSeq+"\" 
// menu_nm=\"신규 추가\" menu_depth=\"0\" menu_url=\"\" menu_type=\"\" 
// view_stat=\"Y\" user_write=\"\" use_yn=\"Y\">"+
		var menu_seqs = 		"<input name='menu_seqs' type='text' value='";
		var menu_nms = 		"<input name='menu_nms' type='text' value='";
		var menu_depths = 	"<input name='menu_depths' type='text' value='";
		var menu_urls = 		"<input name='menu_urls' type='text' value='";
		var menu_types = 		"<input name='menu_types' type='text' value='";
		var view_stats =			"<input name='view_stats' type='text' value='";
		var user_writes = 		"<input name='user_writes' type='text' value='";
		var use_yns = 			"<input name='use_yns' type='text' value='";

		$(".dd-item").each(function (i){
			if(i!=0){
				menu_seqs 		+= ";";
				menu_nms 		+= ";";
				menu_depths 	+= ";";
				menu_urls 		+= ";";
				menu_types 		+= ";";
				view_stats 		+= ";";
				user_writes 		+= ";";
				use_yns 			+= ";";				
			}
			menu_seqs 		+= $(this).attr("data-id")==""?" ":$(this).attr("data-id");
			menu_nms 		+= $(this).attr("menu_nm")==""?" ":$(this).attr("menu_nm");
			menu_depths 	+= $(this).attr("menu_depth")==""?" ":$(this).attr("menu_depth");
			menu_urls 		+= $(this).attr("menu_url")==""?" ":$(this).attr("menu_url");
			menu_types 		+= $(this).attr("menu_type")==""?" ":$(this).attr("menu_type");
			view_stats 		+= $(this).attr("view_stat")==""?" ":$(this).attr("view_stat");
			user_writes 		+= $(this).attr("user_write")==""?" ":$(this).attr("user_write");
			use_yns 			+= $(this).attr("use_yn")==""?" ":$(this).attr("use_yn");
		});
		menu_seqs += 		"'>";
		menu_nms += 		"'>";
		menu_depths += 	"'>";
		menu_urls += 		"'>";
		menu_types += 		"'>";
		view_stats +=		"'>";
		user_writes += 		"'>";
		use_yns += 			"'>";
		
		$("#frmSave").append(menu_seqs 	);
		$("#frmSave").append(menu_nms 	);
		$("#frmSave").append(menu_depths );
		$("#frmSave").append(menu_urls 	);
		$("#frmSave").append(menu_types 	);
		$("#frmSave").append(view_stats 	);
		$("#frmSave").append(user_writes );
		$("#frmSave").append(use_yns 	);
		
		$("#frmSave").submit();
	}
	
}

$(document).ready(function()
{
	<c:if test="${'Y'==param.reload }">
	location.href="./menuList.do";
	</c:if>
    $('#nestable3').nestable();
});
</script>